package com.hlhj.carview.aty

import android.Manifest
import android.content.Intent
import com.crush.skipupdate.SplashCallback
import com.crush.skipupdate.SplashRequest
import com.hlhj.carview.BaseAty
import com.hlhj.carview.Iview.NothingView
import com.hlhj.carview.R
import com.hlhj.carview.bean.ItemAskListBean
import com.hlhj.carview.bean.JumpBean
import com.hlhj.carview.http.CustomAction
import com.hlhj.carview.http.MyJsonConvert
import com.hlhj.carview.http.Urls
import com.hlhj.carview.presenter.NothingPresenter
import com.hlhj.carview.utils.SPHelper
import com.lzy.okgo.OkGo
import com.lzy.okgo.convert.StringConvert
import com.lzy.okgo.model.Response
import com.lzy.okrx2.adapter.ObservableResponse
import com.tbruyelle.rxpermissions.RxPermissions
import io.reactivex.schedulers.Schedulers

/**
 *NeverMore
 *2020/9/11
 **/
class IniitAty:BaseAty<NothingView,NothingPresenter>(), NothingView {

    override fun initView() {
        val b = RxPermissions.getInstance(this).isGranted(Manifest.permission.CAMERA)
        if (!b){
            RxPermissions.getInstance(this).request(Manifest.permission.CAMERA
                    )
                    .subscribe {
                        b->
                        Thread(Runnable {
                            Thread.sleep(1500)
                            OkGo.get<String>("http://mock-api.com/NnXaOLKy.mock/appconfig")
                                    .converter(object : StringConvert(){})
                                    .adapt(ObservableResponse<String>())
                                    .subscribeOn(Schedulers.io())
                                    .subscribe(object : CustomAction<Response<String>>(this){
                                        override fun httpSuccess(t: Response<String>) {
                                            SplashRequest.init(this@IniitAty,t.body(),object :SplashCallback{
                                                override fun onStartMainActivity() {
                                                    if (SPHelper.getLogin(this@IniitAty)){
                                                        startActivity(Intent(this@IniitAty,MainActivity::class.java))
                                                    }else{
                                                        startActivity(Intent(this@IniitAty,LoginAty::class.java))

                                                    }

                                                }
                                            })
                                        }
                                    })
                        }).start()
                    }
        }else{
            Thread(Runnable {
                Thread.sleep(1500)
                OkGo.get<String>("http://mock-api.com/NnXaOLKy.mock/appconfig")
                        .converter(object : StringConvert(){})
                        .adapt(ObservableResponse<String>())
                        .subscribeOn(Schedulers.io())
                        .subscribe(object : CustomAction<Response<String>>(this){
                            override fun httpSuccess(t: Response<String>) {
                                SplashRequest.init(this@IniitAty,t.body(),object :SplashCallback{
                                    override fun onStartMainActivity() {
                                        if (SPHelper.getLogin(this@IniitAty)){
                                            startActivity(Intent(this@IniitAty,MainActivity::class.java))
                                        }else{
                                            startActivity(Intent(this@IniitAty,LoginAty::class.java))

                                        }

                                    }
                                })
                            }
                        })
            }).start()
        }

    }

    override fun logicStart() {
    }

    override fun setListener() {
    }

    override fun getContentID(): Int {
        return R.layout.init_aty
    }

    override fun bindView(): NothingView {
        return this
    }

    override fun createPresenter(): NothingPresenter {
        return NothingPresenter()
    }
}